<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../jquery.min.js"></script>
<script type="text/javascript" src="../jquery.easyui.min.js"></script>
<script type="text/javascript" src="../src/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../src/xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../src/xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
	 <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:true" title="West" style="width:160px;">
        	
        	<ul id="tt" style="margin-top: 10px"></ul>
        </div>
        <div style="text-align:center;padding:5px 0">
		     <!-- 类别:<input type="text" name="parentId" id="parentId" style="width: 500px;" /> -->
		     <form method="post" id="newsForm">
		        标题：<input type="text" name="title" id="title">
		        类别：<span id="inputtext"></span>
		        <input type="text" name="cateId" id="cateId" style="display:none">
		        <br/>
		        <textarea cols="120" rows="30" id="content" name="content"></textarea>
		        <br/>
		        
		   </form>
		   <!-- <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-add" onclick="editNews()" plain="true" style="background: #1f637b;text-align: center;display:block;width: 80px;color:#fff;border:none;float: right;margin-left: 10px;">修改</a> -->
		   	<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-add" onclick="addNews()" plain="true" style="background: #1f637b;text-align: center;display: block;width: 80px;color:#fff;border:none;float: right;">添加</a>
		   	
		</div>
    </div>
    
    <script>
    	
    	$('#tt').tree({//加载树节点
    		animate:true,
		    url:'http://localhost:3000/cate/list',
		    method:'get',
		    onClick: function(node){
		    	var p="http://localhost:3000/cate/data/"+node.path.substr(1);
		    	$.ajax({
						type:"post",
						url:p
				}).then(function(res){
					$("#inputtext").html(node.text);
					$("#cateId").val(node._id);//传入当前结点的id 赋予改文章的类型

				});
				
			}
		});
		var Request = new Object();
		id= GetRequest().id;//从地址栏获取传来的id
		if(id){
			$.ajax({
				type:'post',
				url:'http://localhost:3000/news/data/'+id
			}).then(function(res0){
				$.ajax({
					type:'post',
					url:'http://localhost:3000/cate/data/'+res0.cateId
				}).then(function(res1){//将要修改的内容设置到表里
					$('#inputtext').html(res1.text);
					$('#cateId').val(res1._id);
					$('#title').val(res0.title);
					$('#content').val(res0.content);
				});
			});
		}
		if(id){//如果存在id，则是修改内容 
			var http="http://localhost:3000/news/data/"+id;	
		}else{
			var http="http://localhost:3000/news/data";	
		}
		function addNews(){//提交数据
			$('#newsForm').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
						$.ajax({
							type:"post",
							url:http,
							data:$("#newsForm").serializeJSON()
						}).then(function(res){
							$('#newsForm').form('clear');
							$("#content").val('');
							http="http://localhost:3000/news/data";
						});
		            }
		        }
		    });
		}

    	$('#content').xheditor({
	        html5Upload: false,
	        upMultiple:1,


	        upLinkUrl:"/upload.php",
	        upLinkExt:"zip,rar,txt",

	        upImgUrl:"/uploadImg",
	        upImgExt:"jpg,jpeg,gif,png",
	        
	        upFlashUrl:"upload.php",
	        upFlashExt:"swf",
	        
	        upMediaUrl:"upload.php",
	        upMediaExt:"wmv,avi,wma,mp3,mid"
        });
		function GetRequest() {
		    var url = location.search; //获取url中"?"符后的字串
		    var theRequest = new Object();
		    if (url.indexOf("?") != -1) {
		        var str = url.substr(1);
		        strs = str.split("&");
		        for(var i = 0; i < strs.length; i ++) {
		            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		        }
		    }
		    return theRequest;
		}
		
	</script>
</body>
</html>
